﻿app.controller('HomeController', ['$scope', 'LoginFactory', 'ChoresFactory', function ($scope, LoginFactory, ChoresFactory) {
    $scope.greeting = "Hello from Angular Controller";

    // Logged in status
    $scope.status = LoginFactory.status;

    // User's credentials
    $scope.user = {
        username: '',
        password: ''
    };

    // Contains the user's info/role
    $scope.userInfo = {};

    // Contains the chores
    $scope.chores = [];

    // Page location & how many to show
    $scope.page = function (num) {
        console.log(num);
        $scope.currPage = num - 1;
        $scope.amountCounter = $scope.currPage * $scope.amountPerPage;
        $scope.getStuff();
    };
    $scope.currPage = 0;
    $scope.amountCounter = 0;
    $scope.amountPerPage = 5;

    $scope.dologin = function () {
        console.log("dologin??");
        LoginFactory.factorylogin($scope.user).then(function (data) {
            console.log('data: ', data);
            $scope.getUserInfo();
        });
    }

    // After login, get stuff based on user role
    $scope.getStuff = function () {
        if ($scope.userInfo.userType == "Admin") {
            ChoresFactory.getAllChores().then(function (data) {
                $scope.chores = data;
                console.log('get all chores: ', data);
            })
        }
        if ($scope.userInfo.userType == "General") {
            ChoresFactory.getUserChores($scope.amountCounter).then(function (data) {
                console.log('get user chores: ', data);
                $scope.chores = data;
            })
        }
    }

    // Get the user's info
    $scope.getUserInfo = function () {
        LoginFactory.getUserInfo().then(function (data) {
            console.log('getUserInfo data: ', data);
            $scope.userInfo = data;
            $scope.getStuff();
        })
    }

    // Check if user is logged in
    // If so, get the user info
    if (LoginFactory.status.loggedin) {
        console.log('User is logged in');
        $scope.getUserInfo();
    }

    $scope.logout = function () {
        LoginFactory.logout();
    }
}]);